package com.siriframe.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.siriframe.handler.MybatisPlusAllSqlLog;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfiguration {

    // 启用本框架封装的sql语句输出的Bean
    @Bean
    @ConditionalOnProperty(name = {"siriframe.sqllog.show"},havingValue = "true",matchIfMissing = true)
    public MybatisPlusInterceptor mybatisPlusSqlLogInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new MybatisPlusAllSqlLog());
        return mybatisPlusInterceptor;
    }

    // 启用MyBatisPlus分页功能的Bean
    @Bean
    @ConditionalOnProperty(name = {"siriframe.pagination"},havingValue = "true",matchIfMissing = true)
    public MybatisPlusInterceptor mybatisPlusInterceptor2() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    // 启用MyBatisPlus乐观锁插件
    @Bean
    @ConditionalOnProperty(name = {"siriframe.optimistic"},havingValue = "true",matchIfMissing = true)
    public MybatisPlusInterceptor mybatisPlusOptimisticInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}
